//
//  SecondCell.m
//  SweetComics
//
//  Created by LeeZhe on 15/1/31.
//  Copyright (c) 2015年 LeeZhe. All rights reserved.
//

#import "SecondCell.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
@implementation SecondCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.scrollView = [[UIScrollView alloc] init];
        [self.contentView addSubview:_scrollView];
        [_scrollView release];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.style = [[UILabel alloc] init];
        [self.contentView addSubview:_style];
        [_style release];
        self.totalButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.contentView addSubview:_totalButton];
        self.line = [[UIView alloc] init];
        [self.contentView addSubview:_line];
        [_line release];
        
        // 箭头键
        
        self.arrowKey = [[UIImageView alloc] init];
        
        [self.contentView addSubview:_arrowKey];
        
        [_arrowKey release];
        [self createView];
        
        [self setTranslatesAutoresizingMaskIntoConstraints:NO];
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    self.contentView.frame = CGRectMake(7, 0, self.frame.size.width, self.frame.size.height);
    
    _junior.frame = CGRectMake(0, 0,0.107 * self.contentView.frame.size.width,0.15 * self.contentView.frame.size.height);
    
    _scrollView.frame = CGRectMake(0,0.15 * self.contentView.frame.size.height, self.contentView.frame.size.width, self.contentView.frame.size.height * 0.85);
    _style.frame = CGRectMake(0,0,0.267 * self.contentView.frame.size.width,0.15 * self.contentView.frame.size.height); // 漫画类型
    _totalButton.frame =CGRectMake(0.84 * self.contentView.frame.size.width,10,0.1 * self.contentView.frame.size.width,15); // 右侧全部点击按钮
    _line.frame = CGRectMake(0.1867 * self.contentView.frame.size.width,15,0.64 * self.contentView.frame.size.width,0.5); // 分割线
    _line.backgroundColor = [UIColor orangeColor];
    _line.alpha = 0.3;
    _style.font = [UIFont systemFontOfSize:16];
    _style.textColor = [UIColor purpleColor];
    [_totalButton setTitle:@"全部" forState: UIControlStateNormal];
    _totalButton.tintColor = [UIColor darkGrayColor];
    
    _arrowKey.frame = CGRectMake(0.9333 * self.contentView.frame.size.width,8,0.05 * self.frame.size.width,15);
    _arrowKey.image = [UIImage imageNamed:@"箭头"];
    _arrowKey.alpha = 0.3;
    
}
- (void)createView
{
    for (NSInteger i = 0; i < 12; i++) {
        UIImageView *backView = [[UIImageView alloc] initWithFrame:CGRectMake(120 * i, 0,110, 145)];
        backView.image = [UIImage imageNamed:@"kuu.png"];
        [self.scrollView addSubview:backView];
        [backView release];
    }
}


- (void)setComics:(ComicList *)comics
{
  
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:kURLTwo parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        [comics setValuesForKeysWithDictionary:dic];
        for (NSInteger i = 0; i < comics.data.count; i++) {
            
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(120 * i, 0,110, 145)];
            
            self.title = [[UILabel alloc] initWithFrame:CGRectMake(120 * i,150,90,15)];
            self.junior.text = comics.comic_list_title;
            self.title.text = [[comics.data objectAtIndex:i] objectForKey:@"title"];
            
            _title.font = [UIFont systemFontOfSize:12];
            [self.scrollView addSubview:_title];
            // 会话张数
            self.lastup = [[UILabel alloc] initWithFrame:CGRectMake(120 * i + 85, 150, 90, 20)];

            _lastup.font = [UIFont systemFontOfSize:11];
            _lastup.textColor = [UIColor orangeColor];
            _lastup.text = [[comics.data objectAtIndex:i] objectForKey:@"lastup"];
            [self.scrollView addSubview:_lastup];
            [_lastup release];
            NSURL *url = [NSURL URLWithString:[[comics.data objectAtIndex:i] objectForKey:@"cover_url"]];
            [imageView setImageWithURL:url];
            [self.scrollView addSubview:imageView];
            
            NSInteger count = [comics.count integerValue];
            _scrollView.contentSize = CGSizeMake(count * 120, 0);
            
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:@"zhao" object:comics userInfo:nil];
        self.style.text = comics.comic_list_title;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    

  

    
    
}
- (void)dealloc
{
    [_style release];
    [_totalButton release];
    [_line release];
    [_junior release];
    [_scrollView release];
    [_button release];
    [_comics release];
    [_title release];
    [_lastup release];
    [_arrowKey release];
    [super dealloc];

}

@end
